﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Main.Common
{
    public class KeyboardHook
    {
        private int hHook;

        private Win32Api.HookProc KeyboardHookDelegate;

        private List<Keys> preKeysList;

        public event KeyEventHandler OnKeyDownEvent;

        public event KeyEventHandler OnKeyUpEvent;

        public event KeyPressEventHandler OnKeyPressEvent;

        public KeyboardHook()
        {
            Class2.Q91Y6x2zoRBWY();
            this.preKeysList = new List<Keys>();
            //base..ctor();
        }

        public void SetHook()
        {
            this.KeyboardHookDelegate = this.KeyboardHookProc;
            Process currentProcess = Process.GetCurrentProcess();
            ProcessModule mainModule = currentProcess.MainModule;
            IntPtr moduleHandle = Win32Api.GetModuleHandle(mainModule.ModuleName);
            this.hHook = Win32Api.SetWindowsHookEx(13, this.KeyboardHookDelegate, moduleHandle, 0);
        }

        public void UnHook()
        {
            Win32Api.UnhookWindowsHookEx(this.hHook);
        }

        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0 && (this.OnKeyDownEvent != null || this.OnKeyUpEvent != null || this.OnKeyPressEvent != null))
            {
                Win32Api.KeyboardHookStruct keyboardHookStruct = (Win32Api.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.KeyboardHookStruct));
                Keys vkCode = (Keys)keyboardHookStruct.vkCode;
                if ((this.OnKeyDownEvent != null || this.OnKeyPressEvent != null) && (wParam == 256 || wParam == 260) && this.IsCtrlAltShiftKeys(vkCode) && this.preKeysList.IndexOf(vkCode) == -1)
                {
                    this.preKeysList.Add(vkCode);
                }
                if (this.OnKeyDownEvent != null && (wParam == 256 || wParam == 260))
                {
                    KeyEventArgs e = new KeyEventArgs(this.GetDownKeys(vkCode));
                    this.OnKeyDownEvent(this, e);
                }
                if (this.OnKeyPressEvent != null && wParam == 256)
                {
                    byte[] array = new byte[256];
                    Win32Api.GetKeyboardState(array);
                    byte[] array2 = new byte[2];
                    if (Win32Api.ToAscii(keyboardHookStruct.vkCode, keyboardHookStruct.scanCode, array, array2, keyboardHookStruct.flags) == 1)
                    {
                        KeyPressEventArgs e2 = new KeyPressEventArgs((char)array2[0]);
                        this.OnKeyPressEvent(this, e2);
                    }
                }
                if ((this.OnKeyDownEvent != null || this.OnKeyPressEvent != null) && (wParam == 257 || wParam == 261) && this.IsCtrlAltShiftKeys(vkCode))
                {
                    for (int num = this.preKeysList.Count - 1; num >= 0; num--)
                    {
                        if (this.preKeysList[num] == vkCode)
                        {
                            this.preKeysList.RemoveAt(num);
                        }
                    }
                }
                if (this.OnKeyUpEvent != null && (wParam == 257 || wParam == 261))
                {
                    KeyEventArgs e = new KeyEventArgs(this.GetDownKeys(vkCode));
                    this.OnKeyUpEvent(this, e);
                }
            }
            return Win32Api.CallNextHookEx(this.hHook, nCode, wParam, lParam);
        }

        private Keys GetDownKeys(Keys key)
        {
            Keys keys = Keys.None;
            foreach (Keys preKeys in this.preKeysList)
            {
                if (preKeys == Keys.LControlKey || preKeys == Keys.RControlKey)
                {
                    keys |= Keys.Control;
                }
                if (preKeys == Keys.LMenu || preKeys == Keys.RMenu)
                {
                    keys |= Keys.Alt;
                }
                if (preKeys == Keys.LShiftKey || preKeys == Keys.RShiftKey)
                {
                    keys |= Keys.Shift;
                }
            }
            return keys | key;
        }

        private bool IsCtrlAltShiftKeys(Keys key)
        {
            if (key == Keys.LControlKey || key == Keys.RControlKey || key == Keys.LMenu || key == Keys.RMenu || key == Keys.LShiftKey || key == Keys.RShiftKey)
            {
                return true;
            }
            return false;
        }
    }
}
